ggplot2: basicslibrary(tidyverse)
── Attaching packages ───────────────────────────────────────────────────── tidyverse 1.3.0 ──
✓ ggplot2 3.3.0 ✓ purrr 0.3.3
✓ tibble 3.0.0 ✓ dplyr 0.8.5
✓ tidyr 1.0.2 ✓ stringr 1.4.0
✓ readr 1.3.1 ✓ forcats 0.4.0
── Conflicts ──────────────────────────────────────────────────────── tidyverse_conflicts() ──
x dplyr::filter() masks stats::filter()
x dplyr::lag() masks stats::lag()
This notebook contains solutions to all the exercises in Thomas Lin Pederson’s online workshop
We will use an assortment of datasets throughout the document. The purpose is mostly to showcase different plots, and less on getting some divine insight into the world. While not necessary we will call
data(<dataset>)before using a new dataset to indicate the introduction of a new dataset.
We will look at the basic ggplot2 use using the faithful dataset, giving information on the eruption pattern of the Old Faithful geyser in Yellowstone National Park.
library(ggplot2)
data("faithful")
# Basic scatterplot
ggplot(data = faithful, mapping = aes(x = eruptions, y = waiting)) +
geom_point()
# Data and mapping can be given both as global (in ggplot()) or per layer
ggplot() +
geom_point(mapping = aes(x = eruptions, y = waiting), data = faithful)
If an aesthetic is linked to data it is put into
aes()
ggplot(faithful) +
geom_point(aes(x = eruptions, y = waiting, colour = eruptions < 3))
If you simple want to set it to a value, put it outside of
aes()
ggplot(faithful) +
geom_point(aes(x = eruptions, y = waiting), color = 'steelblue1')
Some geoms only need a single mapping and will calculate the rest for you
ggplot(faithful) +
geom_histogram(aes(x = eruptions))
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
All geoms are drawn in the order they are added. The point layer is thus drawn on top of the density contours in the example below.
ggplot(faithful, aes(x = eruptions, y = waiting)) +
geom_density_2d() +
geom_point()
Modify the code below to make the points larger squares and slightly transparent.
See
?geom_pointfor more information on the point layer.
ggplot(faithful) +
geom_point(aes(x = eruptions, y = waiting))
Hint 1: transparency is controlled with
alpha, and shape withshapeHint 2: remember the difference between mapping and setting aesthetics
ggplot(faithful) +
geom_point(aes(x = eruptions, y = waiting), shape = 15, alpha = 0.5, size = 5)
You can see the most common shapes here:
Type ?pch in the console to see more options. For example you can specify ASCII characters using the numbers 33 to 127 or typing each character in between quotation marks.
ggplot(faithful) +
geom_point(aes(x = eruptions, y = waiting), shape = 64, alpha = 0.5, size = 5)
ggplot(faithful) +
geom_point(aes(x = eruptions, y = waiting), shape = "@", alpha = 0.5, size = 5)
Finally, note that shapes between 21 and 25 can be filled with different colors!
ggplot(faithful) +
geom_point(aes(x = eruptions, y = waiting), shape = 21, size = 10,
fill = "skyblue", color = "#FFFF3E",
stroke = 2) ## controls "point" border thickness
Colour the two distributions in the histogram with different colours
Hint 1: For polygons you can map two different colour-like aesthetics:
colour(the colour of the stroke) andfill(the fill colour)
ggplot(faithful) +
geom_histogram(aes(x = eruptions, fill = eruptions >= 3.1), show.legend = FALSE,
color = "white")
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Colour the distributions in the histogram by whether
waitingis above or below60. What happens?
ggplot(faithful) +
geom_histogram(aes(x = eruptions, fill = waiting > 60))
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Change the plot above by setting
position = 'dodge'ingeom_histogram()(while keeping the colouring bywaiting). What dopositioncontrol?
All layers have a position adjustment that resolves overlapping geoms. See more here
ggplot(faithful) +
geom_histogram(aes(x = eruptions, fill = waiting > 60), position = "dodge")
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Add a line that separates the two point distributions. See
?geom_ablinefor how to draw straight lines from a slope and intercept.
ggplot(faithful) +
geom_point(aes(x = eruptions, y = waiting)) +
geom_abline(intercept = 125, slope = -80/4)
We will use the
mpgdataset giving information about fuel economy on different car models.
Every geom has a stat. This is why new data (
count) can appear when usinggeom_bar().
data("mpg")
ggplot(mpg) +
geom_bar(aes(x = class))
The stat can be overwritten. If we have precomputed count we don’t want any additional computations to perform and we use the
identitystat to leave the data alone
library(dplyr)
mpg_counted <- mpg %>%
count(class, name = 'count')
ggplot(mpg_counted) +
geom_bar(aes(x = class, y = count), stat = 'identity')
Most obvious
geom+statcombinations have a dedicated geom constructor. The one above is available directly asgeom_col()
ggplot(mpg_counted) +
geom_col(aes(x = class, y = count))
Values calculated by the stat is available with the
after_stat()function insideaes(). You can do all sorts of computations inside that.
ggplot(mpg) +
geom_bar(aes(x = class, y = after_stat(100 * count / sum(count)))) + ## WTF!
labs(y = "%")
Many stats provide multiple variations of the same calculation, and provides a default (here,
density)
ggplot(mpg) +
geom_density(aes(x = hwy))
While the others must be used with the
after_stat()function
ggplot(mpg) +
geom_density(aes(x = hwy, y = after_stat(scaled)))
While most people use
geom_*()when adding layers, it is just as valid to add astat_*()with an attached geom.Look at
geom_bar()and figure out which stat it uses as default. Then modify the code to use the stat directly instead (i.e. addingstat_*()instead ofgeom_bar())
ggplot(mpg) +
stat_count(aes(x = class))
Use
stat_summary()to add a red dot at the meanhwyfor each groupHint: You will need to change the default geom of
stat_summary()
ggplot(mpg) +
geom_jitter(aes(x = class, y = hwy), width = 0.2)
ggplot(mpg, aes(x = class, y = hwy)) +
geom_jitter(width = 0.2) +
stat_summary(fun = mean, geom = "point", color = "red", size = 3)
Scales define how the mapping you specify inside
aes()should happen. All mappings have an associated scale even if not specified.You can take control by adding one explicitly. All scales follow the same naming conventions.
ggplot(mpg) +
geom_point(aes(x = displ, y = hwy, colour = class)) +
scale_colour_brewer(type = 'qual') ## !
Positional mappings (
xandy) also have associated scales.
ggplot(mpg) +
geom_point(aes(x = displ, y = hwy)) +
scale_x_continuous(breaks = c(3, 5, 6)) +
scale_y_continuous(trans = "log10")
Use
RColorBrewer::display.brewer.all()to see all the different palettes from Color Brewer and pick your favourite. Modify the code below to use it
ggplot(mpg) +
geom_point(aes(x = displ, y = hwy, color = class)) +
scale_colour_brewer(type = "qual", palette = "Spectral")
Modify the code below to create a bubble chart (scatterplot with size mapped to a continuous variable) showing
cylwith size. Make sure that only the present amount of cylinders (4, 5, 6, and 8) are present in the legend.
ggplot(mpg) +
geom_point(aes(x = displ, y = hwy, colour = class)) +
scale_colour_brewer(type = 'qual')
Hint: The
breaksargument in the scale is used to control which values are present in the legend.
ggplot(mpg) +
geom_point(aes(x = displ, y = hwy, colour = class, size = cyl)) +
scale_colour_brewer(type = 'qual') +
scale_size(breaks = c(4, 5, 6, 8))
Note that the increase from 4 to 5 looks bigger than the increase from 5 to 6 (it’s not). However, it does look weird. We can fix this by giving “0” an area of zero.
ggplot(mpg) +
geom_point(aes(x = displ, y = hwy, colour = class, size = cyl)) +
scale_colour_brewer(type = 'qual') +
scale_size_area(breaks = c(4, 5, 6, 8)) ## ensures 0 is mapped to 0 size
Explore the different types of size scales available in ggplot2. Is the default the most appropriate here?
scale_size()
scale_radius() (area size increases exponentially, not recommended)
scale_size_binned()
scale_size_area()
scale_size_binned_area()
Modify the code below so that colour is no longer mapped to the discrete
classvariable, but to the continuousctyvariable. What happens to the guide?
ggplot(mpg) +
geom_point(aes(x = displ, y = hwy, colour = class, size = cty))
ggplot(mpg) +
geom_point(aes(x = displ, y = hwy, color = class, size = cty))
We have a gradient color legend and a size legend with breaks 10, 15, 20, 25, etc.
The type of guide can be controlled with the
guideargument in the scale, or with theguides()function. Continuous colours have a gradient colour bar by default, but setting it tolegendwill turn it back to the standard look. What happens when multiple aesthetics are mapped to the same variable and uses the guide type?
The guides are integrated!
ggplot(mpg) +
geom_point(aes(x = displ, y = hwy, colour = cty, size = cty)) +
scale_color_gradient(guide = "legend")
ggplot(mpg) +
geom_point(aes(x = displ, y = hwy, colour = cty, size = cty)) +
guides(color = "legend")
The facet defines how data is split among panels. The default facet (
facet_null()) puts all the data in a single panel, whilefacet_wrap()andfacet_grid()allows you to specify different types of small multiples
ggplot(mpg) +
geom_point(aes(x = displ, y = hwy)) +
facet_wrap(~ class)
ggplot(mpg) +
geom_point(aes(x = displ, y = hwy)) +
facet_grid(year ~ drv)
One of the great things about facets is that they share the axes between the different panels. Sometimes this is undesirable though, and the behaviour can be changed with the
scalesargument.Usually the space occupied by each panel is equal. This can create problems when different scales are used. Modify the code below so that the y scale differs between the panels in the plot. What happens?
ggplot(mpg) +
geom_bar(aes(y = manufacturer)) +
facet_grid(class ~ .)
ggplot(mpg) +
geom_bar(aes(y = manufacturer)) +
facet_grid(class ~ ., scales = "free_y")
Use the
spaceargument infacet_grid()to change the plot above so each bar has the same width again.
ggplot(mpg) +
geom_bar(aes(y = manufacturer)) +
facet_grid(class ~ ., scales = "free_y", space = "free") +
theme(strip.text.y = element_text(angle = 0)) ## bonus
Facets can be based on multiple variables by adding them together. Try to recreate the same panels present in the plot below by using
facet_wrap()
ggplot(mpg) +
geom_point(aes(x = displ, y = hwy)) +
facet_grid(year ~ drv)
ggplot(mpg) +
geom_point(aes(x = displ, y = hwy)) +
facet_wrap(~ year + drv)
The coordinate system is the fabric you draw your layers on in the end. The default
coord_cartesionprovides the standard rectangular x-y coordinate system.Changing the coordinate system can have dramatic effects.
ggplot(mpg) +
geom_bar(aes(x = class)) +
coord_polar()
Polar coordinates interpret x and y as radius and angle.
ggplot(mpg) +
geom_bar(aes(x = class)) +
coord_polar(theta = 'y') +
expand_limits(y = 70)
You can zoom both on the scale…
ggplot(mpg) +
geom_bar(aes(x = class)) +
scale_y_continuous(limits = c(0, 40))
Warning: Removed 3 rows containing missing values (geom_bar).
and in the coord. You usually want the latter as it avoids changing the plotted data
ggplot(mpg) +
geom_bar(aes(x = class)) +
coord_cartesian(ylim = c(0, 40))
This happens because scales transform data at the begining, whereas coordinates transform data at the end.
In the same way as limits can be set in both the positional scale and the coord, so can transformations, using
coord_trans(). Modify the code below to apply a log transformation to the y axis; first usingscale_y_continuous(), and then usingcoord_trans(). Compare the results — how do they differ?
ggplot(mpg) +
geom_point(aes(x = hwy, y = displ))
ggplot(mpg) +
geom_point(aes(x = hwy, y = displ)) +
scale_y_continuous(trans = "log")
ggplot(mpg) +
geom_point(aes(x = hwy, y = displ)) +
coord_trans(y = "log") ## this one looks better
Coordinate systems are particularly important in cartography. While we will not spend a lot of time with it in this workshop, spatial plotting is well supported in ggplot2 with
geom_sf()andcoord_sf()(which interfaces with the sf package). The code below produces a world map. Try changing thecrsargument incoord_sf()to be'+proj=robin'(This means using the Robinson projection).
data("world", package = "spData")
ggplot(world) +
geom_sf()
ggplot(world) +
geom_sf() +
coord_sf(crs = "+proj=robin")
ggplot(world) +
geom_sf() +
coord_sf(crs = "+proj=laea +x_0=0 +y_0=0 +lon_0=-74 +lat_0=40")
Maps are a huge area in data visualisation and simply too big to cover in this workshop. If you want to explore further I advice you to explore the r-spatial wbsite as well as the website for the sf package
Theming defines the feel and look of your final visualisation and is something you will normally defer to the final polishing of the plot. It is very easy to change looks with a prebuild theme,
ggplot(mpg) +
geom_bar(aes(y = class)) +
facet_wrap(~year) +
theme_minimal()
Further adjustments can be done in the end to get exactly the look you want
ggplot(mpg) +
geom_bar(aes(y = class)) +
facet_wrap(~year) +
labs(title = "Number of car models per class",
caption = "source: http://fueleconomy.gov",
x = NULL,
y = NULL) +
scale_x_continuous(expand = c(0, NA)) +
theme_minimal() +
theme(
text = element_text('Avenir Next Condensed'),
strip.text = element_text(face = 'bold', hjust = 0),
plot.caption = element_text(face = 'italic'),
panel.grid.major = element_line('white', size = 0.5),
panel.grid.minor = element_blank(),
panel.grid.major.y = element_blank(),
panel.ontop = TRUE
)
Themes can be overwhelming, especially as you often try to optimise for beauty while you learn. To remove the last part of the equation, the exercise is to take the plot given below and make it as hideous as possible using the theme function. Go absolutely crazy, but take note of the effect as you change different settings.
g <- ggplot(mpg) +
geom_bar(aes(y = class, fill = drv)) +
facet_wrap(~year) +
labs(title = "Number of car models per class",
caption = "source: http://fueleconomy.gov",
x = 'Number of cars',
y = NULL)
g + theme(
text = element_text("Comic Sans MS", color = "orange"),
axis.text = element_text(color = "white"),
panel.background = element_rect("yellow"),
legend.background = element_rect(fill = "yellow", linetype = "dotted",
color = "white", size = 2),
strip.text = element_text(face = 'bold', hjust = 1, angle = 20),
plot.background = element_rect("black")
)